<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rP = validate($_GET, 'category:n|pg:n');
$oTpl->assign('rP', $rP);
$sWhere = "TRUE";

/// 类别目录 ///
$oMainCtgryList = new uCategoryList();
$oMainCtgryList->cfgWhere("parent_id=0");
$oMainCtgryList->load();
while ($oMainCtgry = $oMainCtgryList->nextItem()) {
	$oSubCtgryList = new uCategoryList();
	$oSubCtgryList->cfgWhere("parent_id='".$oMainCtgry->getID()."'");
	$oSubCtgryList->load();
	$oMainCtgry->set('SubCtgrys', $oSubCtgryList->fetchArray());
}
$oTpl->assign('rMainCtgrys', $oMainCtgryList->fetchArray());
/// 当前类别 ///
$oCtgry = new uCategory($rP['category']);
if (0 < $oCtgry->getID()) {
	$oCtgry->set('BreadCrumb', $oCtgry->fetchBreadCrumb());
	$oTpl->assign('rCtgry', $oCtgry->getData());
	$sWhere .= " AND category_id IN ('".implode("','", $oCtgry->getTreeIdsByTop($oCtgry->getID()))."')";
}

/// 作品列表 ///
$rPager = array(
	'Sze' => 5,
	'Req' => 'pg',
	'Num' => max(1, $rP['pg']),
    'Bse' => http_build_query(array_slice($rP, 0, -1)),
);
$oWorkList = new uWorkList();
$oWorkList->cfgWhere($sWhere);
$rPager['Cnt'] = $oWorkList->countAll();
$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
$oWorkList->cfgOrder("id DESC");
$oWorkList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
while ($oWork = $oWorkList->nextItem()) {
	$oUser = new uUser($oWork->get('owner_id'), 'nick');
	$oWork->set('owner_nick', $oUser->get('nick'));
}
$oTpl->assign('rWorks', $oWorkList->fetchArray());
$oTpl->assign('rPager', $rPager);

$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
